﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLSV
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void them_Click(object sender, EventArgs e)
        {
            //hien panelDB
            panelDB.Enabled = true;
            panelbtn.Enabled = false;
            oksua.Enabled = false;
            okthem.Enabled = true;
        }

        private void sua_Click(object sender, EventArgs e)
        {
            //hien panelDB
            panelDB.Enabled = true;
            panelbtn.Enabled = false;
            okthem.Enabled = false;
            oksua.Enabled = true;


            //dua du lieu tu do`ng dc chon trong GridView len cac textbox
            int r = dataGridView1.CurrentCell.RowIndex;
            txtmssv.Text = dataGridView1.Rows[r].Cells[0].Value.ToString();
            txtten.Text = dataGridView1.Rows[r].Cells[1].Value.ToString();

            dt_ns.Text = dataGridView1.Rows[r].Cells[2].Value.ToString();
            txtlop.Text = dataGridView1.Rows[r].Cells[3].Value.ToString();
            
        }
        private void refresh()
        {
         //ham nay dung de show tat ca cac sinh vien vao gridview  
            DataTable dt= SinhVienDAO.DAO_viewAllSV();
            dataGridView1.DataSource = dt;
        }
        private void Empty()//xoa trong cac text box, an? panelDB
        {
            txtlop.Text = txtmssv.Text = txtten.Text = "";
            dt_ns.Value = DateTime.Now;
            panelbtn.Enabled = true;
            panelDB.Enabled = false;
        }
        private void xoa_Click(object sender, EventArgs e)
        {
            //dat ham xoa vao day (xoa SV duoc chon trong Gridview)
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void okthem_Click(object sender, EventArgs e)
        {
            SinhVienDTO sv = new SinhVienDTO();
            sv.MSSV = Int32.Parse(txtmssv.Text);
            sv.HoTen = txtten.Text;
            sv.NgaySinh = dt_ns.Value;
            sv.DiaChi = txtlop.Text;
            SinhVienDAO.DAO_ThemSV(sv);
            refresh();
            Empty();
        }

        private void oksua_Click(object sender, EventArgs e)
        {
            //dat ham sua SV vao day
            try
            {

                QLSV.SinhVienDTO sv = new SinhVienDTO();
                sv.MSSV = Int32.Parse(txtmssv.Text);
                sv.HoTen = txtten.Text;
                sv.NgaySinh = dt_ns.Value;
                sv.DiaChi = txtlop.Text;
                SinhVienDAO.UpdateSV(sv);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thong bao loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Sua thanh cong", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
            refresh();
            Empty();
            panelDB.Enabled = false;
            panelbtn.Enabled = true;
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            Empty();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            refresh();
            panelDB.Enabled = false;
        }

    }
}
